package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.bo.SpuBo;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.Spu;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.item.service.GoodsService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;


    /**
     * 查询商品列表的方法，返回带分页的结果集对象PageResult
     *
     * @param key
     * @param saleable
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuBoByPage( // 返回值是自己写的分页对象
                                                               // 搜素的关键字，不必填
                                                               @RequestParam(value = "key", required = false) String key,
                                                               // 是否上架
                                                               @RequestParam(value = "saleable", required = false) Boolean saleable,
                                                               @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                               @RequestParam(value = "rows", defaultValue = "5") Integer rows
    ) {
        // 封装到自己写的PageResult工具类里
        PageResult<SpuBo> ResultSpuBos = this.goodsService.querySpuBoByPage(key, saleable, page, rows);
        if (CollectionUtils.isEmpty(ResultSpuBos.getItems())) {
            ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(ResultSpuBos);
    }

    /**
     * 保存商品
     *
     * @param spuBo：提交的json数据里包含了所有的商品信息包含spu,sku 还有扩展的spuDetail信息还有stock库存信息
     * @return ：返回为空
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo) {
        this.goodsService.saveGoods(spuBo);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询商品spu的SpuDetail的方法,用于回显商品
     *
     * @param pid
     * @return
     */
    @GetMapping("spu/detail/{SpuId}")
    public ResponseEntity<SpuDetail> querySpuDetail(@PathVariable("SpuId") Long pid) {
        SpuDetail spuDetail = this.goodsService.querySpuDetailByPid(pid);
        if (spuDetail == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spuDetail);

    }

    /**
     * 查询商品sku的方法,用于回显商品
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> querySku(@RequestParam("id") Long pid) {
        List<Sku> skus = this.goodsService.querySkuByPid(pid);
        if (CollectionUtils.isEmpty(skus)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(skus);
    }

    /**
     * 修改商品：修改商品的话spu可以改，但是sku的话只能删除。因为有可能之前存的sku属性都不存在。没法都update
     *
     * @param spuBo
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity<Void> editGoods(@RequestBody SpuBo spuBo) {
        this.goodsService.editGoods(spuBo);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 删除商品的方法
     *
     * @param
     * @return
     */
    @DeleteMapping("spu/detail/{pid}")
    public ResponseEntity<Void> delGoods(@PathVariable("pid") Long pid) {
        // 要根据pid删4张表 spu\spuDetail\sku\stock
        this.goodsService.delGoods(pid);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 上架商品
     *
     * @return
     */
    @PutMapping("spu/up/{pid}")
    public ResponseEntity<Void> upSpu(@PathVariable("pid") Long pid) {
        this.goodsService.upSpu(pid);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 下架商品
     *
     * @return
     */
    @PutMapping("spu/down/{pid}")
    public ResponseEntity<Void> downSpu(@PathVariable("pid") Long pid) {
        this.goodsService.downSpu(pid);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /*
        根据spu的ID查询Spu
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable Long id) {
        Spu spu = this.goodsService.querySpuById(id);
        if (spu == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spu);
    }

    /*
        根据id查询sku的数据，用于购物车微服务查询商品
     */
    @GetMapping("sku/{id}")
    public ResponseEntity<Sku> querySkuById(@PathVariable("id") Long id) {
        Sku sku =  this.goodsService.querySkuById(id);
        if(sku == null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(sku);
    }


}
